<template>
  <el-card class="box-card">
    <template #header>
      <el-form :model="filterParams" ref="filterForm">
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="filterParams.phone"></el-input>
        </el-form-item>
        <el-form-item label="注册时间" prop="rangeDate">
          <el-date-picker
            v-model="filterParams.rangeDate"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item>
          <el-button :disabled="loading" @click="handleReset">重置</el-button>
          <el-button type="primary" @click="handleQuery" :disabled="loading">查询</el-button>
        </el-form-item>
      </el-form>
    </template>
    <el-table :data="users" style="width: 100%;" v-loading="loading">
      <el-table-column prop="id" label="用户ID" width="100"> </el-table-column>
      <el-table-column prop="name" label="头像" width="80">
        <template v-slot="scope">
          <img
            width="30px"
            :src="
              scope.row.portrait ||
              'https://cube.elemecdn.com/9/c2/f0ee8a3c7c9638a54940382568c9dpng.png'
            "
          />
        </template>
      </el-table-column>
      <el-table-column prop="name" label="用户名" width="120"> </el-table-column>
      <el-table-column prop="phone" label="手机号" width="120"> </el-table-column>
      <el-table-column prop="createTime" label="注册时间" width="120"> </el-table-column>
      <el-table-column prop="address" label="操作">
        <template v-slot="scope">
          <el-button type="text" @click="handleSelectRole(scope.row)">分配角色</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="分配角色" v-model="dialogVisible" width="50%">
      <el-select v-model="roleIdList" multiple placeholder="请选择">
        <el-option v-for="item in roles" :key="item.id" :label="item.name" :value="item.id">
        </el-option>
      </el-select>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleAllocRole">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </el-card>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import { getUserPages, forbidUser } from '/@/services/User'
import { ElForm, ElMessage } from 'element-plus'
import { getAllRoles, allocateUserRoles, getUserRoles } from '/@/services/Role'

export default defineComponent({
  name: 'UserList',
  setup() {
    const users = ref([])
    const filterParams = ref({
      currentPage: 1,
      pageSize: 100,
      phone: '',
      startCreateTime: '',
      endCreateTime: '',
      rangeDate: []
    })
    const loading = ref(true)
    const dialogVisible = ref(false)
    const roles = ref([])
    const roleIdList = ref([])
    const currentUser = ref(null)

    const loadUsers = ref(async () => {
      loading.value = true
      const { rangeDate } = filterParams.value
      if (rangeDate && rangeDate.length) {
        filterParams.value.startCreateTime = rangeDate[0]
        filterParams.value.endCreateTime = rangeDate[1]
      } else {
        filterParams.value.startCreateTime = ''
        filterParams.value.endCreateTime = ''
      }
      const { data } = await getUserPages(filterParams.value)
      users.value = data.data.records
      loading.value = false
    })

    const handleForbidUser = ref(async (user: any) => {
      const { data } = await forbidUser(user.id)
      console.log(data)
    })

    const handleQuery = ref(() => {
      filterParams.value.currentPage = 1
      loadUsers.value()
    })

    const filterForm = ref(null)
    const handleReset = ref(() => {
      const form = filterForm.value as ElForm
      form.resetFields()
      loadUsers.value()
    })

    const handleSelectRole = ref(async (role: any) => {
      currentUser.value = role
      // 加载角色列表
      const { data } = await getAllRoles()
      roles.value = data.data

      const {
        data: { data: userRoles }
      } = await getUserRoles((currentUser.value as any).id)
      roleIdList.value = userRoles.map((item: any) => item.id)

      // 展示对话框
      dialogVisible.value = true
    })

    const handleAllocRole = ref(async () => {
      const { data } = await allocateUserRoles({
        userId: (currentUser.value as any).id,
        roleIdList: roleIdList.value
      })
      if (data.code === '000000') {
        ElMessage.success('操作成功')
      } else {
        ElMessage.error('操作失败')
      }
      dialogVisible.value = false
    })

    return {
      users,
      filterParams,
      loading,
      dialogVisible,
      roles,
      roleIdList,
      currentUser,
      loadUsers,
      handleForbidUser,
      handleQuery,
      filterForm,
      handleReset,
      handleSelectRole,
      handleAllocRole
    }
  },
  created() {
    this.loadUsers()
  }
})
</script>

<style lang="scss" scoped></style>
